package com.acegear.horizon.domain.models.jpa;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.acegear.horizon.domain.models.constraint.ClubEventState;
import com.acegear.horizon.domain.models.constraint.ClubEventType;
import com.acegear.horizon.domain.models.jpa.convert.ListLongConverter;
import com.acegear.horizon.domain.models.jpa.convert.SignupItemConverter;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * Created by wangsike on 2016/12/21.
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
public class ClubEventBase {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Long id;

    @Column(unique = true)
    private Long clubEventId;

    @Column
    private Long ownerId; // 主办方

    @Column(columnDefinition = "TEXT")
    @Convert(converter = ListLongConverter.class)
    private List<Long> sponsors;

    @Column
    private String name;

    @Column
    private String servicePhone;

    @Column
    private Long startAt;

    @Column
    private Long endAt;

    @Column(columnDefinition = "TEXT")
    private String summary;

    @Column
    private String cover;

    @OneToOne(cascade = CascadeType.ALL)
    private Address address;

    @OneToOne(cascade = CascadeType.ALL)
    private Location location;

    @Column
    @Enumerated(EnumType.ORDINAL)
    private ClubEventState state;

    @Column
    @Enumerated
    private ClubEventType type;

    private boolean deleted;

    @CreatedDate
    @Column
    private Long createAt;

    @LastModifiedDate
    @Column
    private Long updateAt;
    
    @Column(columnDefinition = "TEXT")
    @Convert(converter = SignupItemConverter.class)
    private List<SignupItem> signupItems;

    public ClubEventBase() {
    }

    public ClubEventBase(Long clubEventId,
                         Long ownerId,
                         List<Long> sponsors,
                         String name,
                         Long startAt,
                         Long endAt,
                         String summary,
                         String cover,
                         String servicePhone,
                         Address address,
                         Location location,
                         ClubEventType eventType,boolean deleted) {
        this.clubEventId = clubEventId;
        this.ownerId = ownerId;
        this.sponsors = sponsors;
        this.name = name;
        this.startAt = startAt;
        this.endAt = endAt;
        this.summary = summary;
        this.cover = cover;
        this.servicePhone = servicePhone;
        this.address = address;
        this.location = location;
        this.type = eventType;
        this.state = ClubEventState.SIGNUP;
        this.deleted=deleted;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getClubEventId() {
        return clubEventId;
    }

    public void setClubEventId(Long clubEventId) {
        this.clubEventId = clubEventId;
    }

    public Long getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(Long ownerId) {
        this.ownerId = ownerId;
    }

    public List<Long> getSponsors() {
        return sponsors;
    }

    public void setSponsors(List<Long> sponsors) {
        this.sponsors = sponsors;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getStartAt() {
        return startAt;
    }

    public void setStartAt(Long startAt) {
        this.startAt = startAt;
    }

    public Long getEndAt() {
        return endAt;
    }

    public void setEndAt(Long endAt) {
        this.endAt = endAt;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public ClubEventState getState() {
        Long current = System.currentTimeMillis();
        if (current < endAt) {
            state = ClubEventState.SIGNUP;
        } else {
            state = ClubEventState.END;
        }
        return state;
    }

    public void setState(ClubEventState state) {
        this.state = state;
    }

    public void eventEnd() {
        this.state = ClubEventState.END;
    }

    public ClubEventType getType() {
        return type;
    }

    public void setType(ClubEventType type) {
        this.type = type;
    }

    public String getServicePhone() {
        return servicePhone;
    }

    public void setServicePhone(String servicePhone) {
        this.servicePhone = servicePhone;
    }

    public Long getCreateAt() {
        return createAt;
    }

    public Long getUpdateAt() {
        return updateAt;
    }
    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

	public List<SignupItem> getSignupItems() {
		return signupItems;
	}

	public void setSignupItems(List<SignupItem> signupItems) {
		this.signupItems = signupItems;
	}

	public void setCreateAt(Long createAt) {
		this.createAt = createAt;
	}

	public void setUpdateAt(Long updateAt) {
		this.updateAt = updateAt;
	}
}
